package com.nl.controller.common.exception;

import com.nl.controller.common.global.GlobalEnum;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response handle(Exception e) {
        Response response;
        if (e instanceof SmsException) {
            SmsException userException = (SmsException) e;
            response =  ResponseUtil.error(userException.getCode(),
                    userException.getMessage());
        } else {
            log.error("系统异常：{}", e);
            response = ResponseUtil.error(GlobalEnum.SUCC.UNKOWN_ERROR);
        }
        return response;
    }
}
